﻿using Microsoft.SharePoint.Administration;
using System;
using System.Collections.Generic;
using SP.FeatureAdministrationTool.Extensions;
using Microsoft.SharePoint;

namespace SP.FeatureAdministrationTool.SiteCollectionModule
{
    public class SiteCollectionManager
    {
        public static List<SiteCollection> GetSiteCollections(Guid webApplicationId)
        {
            SPWebApplication webApp = SPFarm.Local.GetAllWebApplications().Find(a => a.Id == webApplicationId);
            if (webApp == null)
            {
                throw new SPException("The selected web application doesn't exist.");
            }

            List<SiteCollection> siteCollections = new List<SiteCollection>();
            foreach (SPSite site in webApp.Sites)
            {
                try
                {
                    siteCollections.Add(new SiteCollection()
                    {
                        Id = site.ID,
                        Title = site.RootWeb.Title
                    });
                }
                catch
                {
                }
            }

            return siteCollections;
        }
    }
}
